package by.artureg.so.dl;

import by.artureg.so.conf.ConfLoader;
import by.artureg.so.conf.Report;
import by.artureg.so.ex.SoException;
import by.artureg.so.to.ColumnTo;
import by.artureg.so.to.ParamTo;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;


@Service
public class SoService {

    @Inject
    private SoDao dao;

    public List<List<ColumnTo>> runReport(String reportName, List<ParamTo> values) throws SoException {
        Report report = getReportByName(reportName);
        return dao.request(report, values);
    }

    public Report getReportByName(String name) throws SoException {
        List<Report> reports = getReportList();
        for (Report report : reports) {
            if (report.getName().equals(name)) {
                return report;
            }
        }

        throw new SoException("Report with name '" + name + "' doesn't found");
    }


    public List<Report> getReportList() throws SoException {
        return ConfLoader.load().getReports();
    }

}
